package com.interfaceb2b.apps.gtddroid.list.activity.task;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.google.inject.Inject;
import com.interfaceb2b.apps.gtddroid.R;
import com.interfaceb2b.apps.gtddroid.core.model.Task;
import com.interfaceb2b.apps.gtddroid.core.model.persistence.TaskPersister;
import com.interfaceb2b.apps.gtddroid.core.view.MenuUtils;
import com.interfaceb2b.apps.gtddroid.list.annotation.Inbox;
import com.interfaceb2b.apps.gtddroid.list.config.ListConfig;
import com.interfaceb2b.apps.gtddroid.list.config.TaskListConfig;
import com.interfaceb2b.apps.gtddroid.preference.model.Preferences;

public class InboxActivity extends AbstractTaskListActivity {

    @Inject private TaskPersister mTaskPersister;

    @Inject @Inbox
    private TaskListConfig mTaskListConfig;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
		mOtherButton.setText(R.string.clean_inbox_button_title);
		Drawable cleanIcon = getResources().getDrawable(R.drawable.edit_clear);
		cleanIcon.setBounds(0, 0, 24, 24);
		mOtherButton.setCompoundDrawables(cleanIcon, null, null, null);
		mOtherButton.setVisibility(View.VISIBLE);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuUtils.addCleanInboxMenuItem(menu);
        super.onCreateOptionsMenu(menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MenuUtils.CLEAN_INBOX_ID:
        	doCleanup();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected ListConfig<Task> createListConfig()
	{
        return mTaskListConfig;
	}

	@Override
	protected void onOtherButtonClicked() {
		doCleanup();
	}
	
	private void doCleanup() {
    	Preferences.cleanUpInbox(this);
        Toast.makeText(this, R.string.clean_inbox_message, Toast.LENGTH_SHORT).show();
    	// need to restart the activity since the query has changed
    	// mCursor.requery() not enough
    	startActivity(new Intent(this, InboxActivity.class));
		finish();
	}
}
